package com.hoperun.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;

import com.hoperun.bean.AdminManagerRoles;
import com.hoperun.dao.AdminManagerRolesDAO;
import com.hoperun.framework.ibatis.page.BootGrid;
import com.hoperun.framework.ibatis.page.BootGridPage;
import com.hoperun.framework.query.Criteria;
import com.hoperun.framework.util.Constant;
import com.hoperun.service.IAdminManagerRolesSV;


@Service("adminManagerRolesSV")
public class AdminManagerRolesSVImpl implements IAdminManagerRolesSV
{     
	@Resource
	private AdminManagerRolesDAO adminManagerRolesDAO;

	@Override
	public void delete(Long id) throws Exception
	{
		adminManagerRolesDAO.delete(id);
	}

	@Override
	public AdminManagerRoles queryById(Long id) throws Exception
	{
		return adminManagerRolesDAO.queryById(id);
	}

	@Override
	public AdminManagerRoles save(AdminManagerRoles adminManagerRoles) throws Exception
	{
		adminManagerRolesDAO.insert(adminManagerRoles);
		return adminManagerRoles;
	}

	@Override
	public void update(AdminManagerRoles adminManagerRoles) throws Exception
	{
		adminManagerRolesDAO.update(adminManagerRoles);
	}

	@Override
	public void saveOrUpdate(AdminManagerRoles adminManagerRoles)throws Exception
	{
		if(adminManagerRoles == null)
            return;
		if(StringUtils.isNotBlank(""+ adminManagerRoles.getManagerId()))
		{
			update(adminManagerRoles);
		}
		else
		{
			save(adminManagerRoles);
		}
	}

	@Override
	public void saveOrUpdate(List<AdminManagerRoles> list)throws Exception
	{
		if(CollectionUtils.isNotEmpty(list))
		{
			for(AdminManagerRoles adminManagerRoles : list)
			{
				saveOrUpdate(adminManagerRoles);
			}
		}
	}

	@Override
	public List<AdminManagerRoles> queryByCondition(Criteria criteria)throws Exception
	{
		return adminManagerRolesDAO.queryByCondition(criteria);
	}
	
	@Override
	public int queryCountByCondition(Criteria criteria)throws Exception
	{
		return adminManagerRolesDAO.queryCountByCondition(criteria);
	}
	
	@Override
	public BootGridPage<AdminManagerRoles> queryPage(Criteria criteria,
			BootGrid<AdminManagerRoles> grid) throws Exception 
	{

	//	AdminManagerRoles model = grid.getEntity();

		int totalCount = adminManagerRolesDAO.queryCountByCondition(criteria);

		if (Constant.ZERO < totalCount) {
			
			Map<String, String> sorts = grid.getSort();

			/** 设置排序规则 **/
			if (MapUtils.isEmpty(sorts)) {
				Set<String> keySets = sorts.keySet();
				String sort = "";
				for (String key : keySets) {
					sort += " , " + key + " " + sorts.get(key);
				}
				criteria.orderBy(sort.replaceFirst(",", ""));
			}
			if(grid.getCurrent() >= Constant.ZERO){
				int start = (grid.getCurrent() - Constant.ONE) * grid.getRowCount();
				int size = grid.getRowCount();
	
				criteria.limit(start, size);
			}
			List<AdminManagerRoles> list = adminManagerRolesDAO
					.queryByCondition(criteria);
			BootGridPage<AdminManagerRoles> page = new BootGridPage<AdminManagerRoles>(grid.getCurrent(), grid.getRowCount(), totalCount, list);

			return page;
		}
        return new BootGridPage<AdminManagerRoles>(grid.getCurrent(), grid.getRowCount(), totalCount, new ArrayList<AdminManagerRoles>());

	}
    
    /**
     * 按条件查询记录记录
     * @param criteria
     * @throws Exception
	 */
    @Override
	public List<AdminManagerRoles> queryBySpecificCondition(Criteria criteria)throws Exception
    {
    	return adminManagerRolesDAO.queryBySpecificCondition(criteria);
	}
    
    /**
	 * 按条件查询记录条数
	 * @param criteria
	 * @throws Exception
	 */
    @Override
	public int queryCountBySpecificCondition(Criteria criteria)throws Exception
    {
        return adminManagerRolesDAO.queryCountBySpecificCondition(criteria);
	}
    
    /**
     *  分页查询
	 * @param criteria
	 * @return
	 * @throws Exception
	 */
    @Override
	public BootGridPage<AdminManagerRoles> querySpecificPage(Criteria criteria,
			BootGrid<AdminManagerRoles> grid) throws Exception
    {

    //	AdminManagerRoles model = grid.getEntity();

		int totalCount = adminManagerRolesDAO.queryCountBySpecificCondition(criteria);

		if (Constant.ZERO < totalCount) {
			
			Map<String, String> sorts = grid.getSort();

			/** 设置排序规则 **/
			if (MapUtils.isNotEmpty(sorts)) {
				Set<String> keySets = sorts.keySet();
				String sort = "";
				for (String key : keySets) {
					sort += " , " + key + " " + sorts.get(key);
				}
				criteria.orderBy(sort.replaceFirst(",", ""));
			}
			if(grid.getCurrent() >= Constant.ZERO){
				int start = (grid.getCurrent() - Constant.ONE) * grid.getRowCount();
				int size = grid.getRowCount();
	
				criteria.limit(start, size);
			}
			List<AdminManagerRoles> list = adminManagerRolesDAO
					.queryBySpecificCondition(criteria);
			BootGridPage<AdminManagerRoles> page = new BootGridPage<AdminManagerRoles>(grid.getCurrent(), grid.getRowCount(), totalCount, list);

			return page;
		}
        return new BootGridPage<AdminManagerRoles>(grid.getCurrent(), grid.getRowCount(), totalCount, new ArrayList<AdminManagerRoles>());

	}
}
